const router = require("koa-router")();
const { query } = require("./../config/dbPool");
const {
  USERLIST_SQL,
  USERROLELIST_SQL,
  USERINFO_SQL,
  USERINFO_SERVE,
  USERADD_SQL,
  USERUPDATE_SQL,
  USERDELETE_SQL,
  PASSWORDUPDATE_SQL,
} = require("./../config/dbSQL");

router.prefix("/user");

/**
 * 用户登录
 * @url /user/login
 * @params { username } web端username对应数据库user_id字段
 * @params { password } 密码
 */
router.post("/login", async (ctx) => {
  const request = ctx.request.body;
  const { username, password } = request;
  const queryData = [username];
  const res = JSON.parse(
    JSON.stringify(await query(USERINFO_SERVE, queryData))
  );
  if (res.result && res.data.length == 0) {
    ctx.body = {
      status: 200,
      result: false,
      message: "用户不存在",
    };
  } else if (res.result && res.data[0].password != password) {
    ctx.body = {
      status: 200,
      result: false,
      message: "密码不正确",
    };
  } else {
    res.data[0].role = [res.data[0].role];
    ctx.body = {
      status: 200,
      result: true,
      data: {
        token: res.data[0].user_id,
        ...res.data[0],
      },
      message: "登录成功",
    };
  }
});

/**
 * 获取全部用户信息
 * @url /user/info
 */
router.post("/list", async (ctx) => {
  const request = ctx.request.body;
  const { user_id, role } = request;
  let res = {};
  if (user_id) {
    res = JSON.parse(JSON.stringify(await query(USERINFO_SQL, [user_id])));
  } else if (role) {
    res = JSON.parse(JSON.stringify(await query(USERROLELIST_SQL, [role])));
  } else {
    res = JSON.parse(JSON.stringify(await query(USERLIST_SQL)));
  }
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: res.data,
      message: "获取成功",
    };
  }
});

/**
 * 根据token获取用户信息
 * @url /user/info
 */
router.get("/info", async (ctx) => {
  const request = ctx.request.body;
  const token = ctx.request.header.token;
  const queryData = [token];
  const res = JSON.parse(JSON.stringify(await query(USERINFO_SQL, queryData)));
  if (res.result && res.data.length == 0) {
    ctx.body = {
      status: 200,
      result: false,
      message: "用户不存在",
    };
    return;
  } else {
    res.data[0].role = [res.data[0].role];
    ctx.body = {
      status: 200,
      result: true,
      data: {
        token: res.data[0].user_id,
        ...res.data[0],
      },
      message: "获取成功",
    };
  }
});

/**
 * 根据user_id获取用户信息
 * @url /user/:user_id/info
 */
router.get("/:user_id/info", async (ctx) => {
  const request = ctx.request.body;
  const user_id = ctx.params.user_id;
  const queryData = [user_id];
  const res = JSON.parse(JSON.stringify(await query(USERINFO_SQL, queryData)));
  if (res.result && res.data.length == 0) {
    ctx.body = {
      status: 200,
      result: false,
      message: "用户不存在",
    };
    return;
  } else {
    res.data[0].role = [res.data[0].role];
    ctx.body = {
      status: 200,
      result: true,
      data: {
        token: res.data[0].user_id,
        ...res.data[0],
      },
      message: "获取成功",
    };
  }
});

/**
 * 注册用户
 * @url /user/register
 */
router.post("/register", async (ctx) => {
  const request = ctx.request.body;
  const { user_id, user_name, user_sex, user_age, user_phone, dept_id, role } =
    request;
  if (user_id) {
    const data = JSON.parse(
      JSON.stringify(await query(USERINFO_SQL, [user_id]))
    );
    if (data.result && data.data.length !== 0) {
      ctx.body = {
        status: 200,
        result: false,
        message: "用户已存在",
      };
      return;
    }
    const queryData = {
      user_id,
      user_name,
      user_sex,
      user_age,
      user_phone,
      dept_id,
      role,
    };
    const res = await query(USERADD_SQL, queryData);
    if (res.result) {
      ctx.body = {
        status: 200,
        result: true,
        message: "注册成功",
      };
    } else {
      ctx.body = {
        status: 200,
        result: false,
        message: "注册失败",
      };
    }
  }
});

/**
 * 修改用户信息
 * @url /user/:user_id/info
 */
router.put(`/:user_id/info`, async (ctx) => {
  const request = ctx.request.body;
  const user_id = ctx.params.user_id;
  const { user_name, user_sex, user_age, user_phone, dept_id } = request;
  const queryData = [
    // user_id,
    user_name,
    user_sex,
    user_phone,
    user_age,
    dept_id,
    // role,
    // password,
    user_id,
  ];
  const res = await query(USERUPDATE_SQL, queryData);
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "修改失败",
    };
  }
});

/**
 * 修改用户密码
 * @url /user/password
 */
router.put(`/password`, async (ctx) => {
  const request = ctx.request.body;
  const { origin_password, password, check_password } = request;
  const token = ctx.request.header.token;
  const data = JSON.parse(JSON.stringify(await query(USERINFO_SERVE, [token])));
  if (data.data[0].password != origin_password) {
    ctx.body = {
      status: 200,
      result: false,
      message: "原密码错误",
    };
    return;
  } else if (data.data[0].password == password) {
    ctx.body = {
      status: 200,
      result: false,
      message: "新密码不能与原密码相同",
    };
    return;
  } else if (check_password != password) {
    ctx.body = {
      status: 200,
      result: false,
      message: "新密码与确认密码不一致",
    };
    return;
  }
  const queryData = [password, token];
  const res = await query(PASSWORDUPDATE_SQL, queryData);
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      message: "修改成功,请重新登录",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "修改失败",
    };
  }
});

/**
 * 删除用户信息
 * @url /user/:user_id/info
 */
router.delete(`/:user_id/info`, async (ctx) => {
  const request = ctx.request.body;
  const user_id = ctx.params.user_id;
  if (user_id) {
    const queryData = [user_id];
    const res = await query(USERDELETE_SQL, queryData);
    if (res.result) {
      ctx.body = {
        status: 200,
        result: true,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        status: 200,
        result: false,
        message: "删除失败",
      };
    }
  }
});

module.exports = router;
